<template>
  <el-dialog title="分配角色" :visible="isShowDialog" @close="btnCancel">
    <el-checkbox-group v-model="checkList">
      <el-checkbox
        v-for="role in roleList"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailByIdApi } from '@/api/user'
import { assignRole } from '@/api/employee'
export default {
  props: {
    isShowDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roleList: [],
      checkList: [],
      userId: ''
    }
  },
  created() {
    this.getRoleList()
  },
  methods: {
    async getRoleList() {
      const { rows } = await getRoleList()
      this.roleList = rows
    },
    // 封装一个回显当前员工数据的函数, 用来显示原来旧有的角色
    async getCheckList(id) {
      this.userId = id
      const { roleIds } = await getUserDetailByIdApi(id)
      this.checkList = roleIds
    },
    async btnOK() {
      await assignRole({
        id: this.userId,
        roleIds: this.checkList
      })
      this.$message.success('修改成功')
      this.$emit('update:isShowDialog', false)
    },
    btnCancel() {
      this.checkList = []
      this.$emit('update:isShowDialog', false)
    }
  }
}
</script>

<style>

</style>
